<?php

namespace App\Http\Controllers\admin;

use App\Models\FangOwner;
use Illuminate\Http\Request;
use OSS\OssClient;
use OSS\Core\OssException;

class FangOwnerController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = FangOwner::paginate($this->paginate);
        return view('admin.fangowner.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.fangowner.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $params = $request->except('_token');

        //表单验证

        //添加入库
        $res = FangOwner::create($params);

        if ($res) {
            return redirect(route('admin.fangowner.index'));
        } else {
            return redirect(route('admin.fangowner.create'))->withErrors('添加失败');
        }


    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show(Fangowner $fangowner)
    {
        $pic = explode('#',trim($fangowner->pic,'#'));

        array_map(function ($val){
            echo "<div style='display: inline-block; margin: 20px'>
               <img src='$val' alt='' width='150' height='150'>
        </div>";
        },$pic);

        return  '';
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $res = FangOwner::destroy($id);

        if($res){
            return  $this->witeJson(0,null,'删除房东成功');
        }else{
            return  $this->witeJson(100001,null,'删除房东失败');

        }
    }

    /**
     * 房东图片上传
     * @param Request $request
     */
    public function localfile(Request $request)
    {
        try {
            $fileName = $request->file('file')->store('', 'fangowner');

            $filePath = '/uploads/fangowner/' . $fileName;

            return $this->witeJson(0, ['pic' => $filePath], '添加成功');
        } catch (\Exception $e) {
            return $this->witeJson(10001, null, "添加失败");
        }

    }

    /**
     * 删除本地图片
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delpic(Request $request)
    {
        $picPhth = $request->get('pic');

        $res = unlink(public_path() . $picPhth);

        if ($res) {
            return $this->witeJson(0, null, '删除成功');
        } else {
            return $this->witeJson(10002, null, '删除失败');

        }

    }

    /**
     * 上传图片到云端
     * @param Request $request
     */
    public function ossfile(Request $request)
    {
        $filePath = $request->file('file')->getPathname();

        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        $accessKeyId = 'LTAI5tKGdFnsYGHudzGYfeN8';
        $accessKeySecret = "1UxHYX4fDgyOR4IVxL2q1LCNFIXzzd";
        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        // 填写Bucket名称，例如examplebucket。
        $bucket = "cyf2008a";
        // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        $object = "fangowner/".md5(rand(1,9).time()).'.png';
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。


        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $res = $ossClient->uploadFile($bucket, $object, $filePath)['info']['url'];
            return $this->witeJson(0,['pic'=>$res],'添加成功');
        } catch (OssException $e) {

            return $this->witeJson(10003,null,'添加失败');
        }

    }


    public function delOssfile(Request $request)
    {
        $filePath = $request->get('pic');

        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        $accessKeyId = "LTAI5tKGdFnsYGHudzGYfeN8";
        $accessKeySecret = "1UxHYX4fDgyOR4IVxL2q1LCNFIXzzd";
        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        // 填写Bucket名称，例如examplebucket。
        $bucket = "cyf2008a";
        // 填写文件完整路径，例如exampledir/exampleobject.txt。文档完整路径中不能包含Bucket名称。
        $object = $filePath;

        $object = substr($object,strpos($object,'.com')+5);

        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $ossClient->deleteObject($bucket, $object);

            return $this->witeJson(0, null, '删除成功');

        } catch(OssException $e) {

            return $this->witeJson(10003,null,'添加失败');
;
        }

    }
}
